//
//  CustomerNavigationBars.m
//  SZBank
//
//  Created by LuoYao on 16/7/11.
//  Copyright © 2016年 LuoYao. All rights reserved.
//  自定义导航条

#import "CustomerNavigationBars.h"

@interface CustomerNavigationBars ()<UIScrollViewDelegate>
{
    CGFloat _cKwidth,_cKheight;
}
@property (nonatomic) UIScrollView *scrollView;
@property (nonatomic) NSMutableArray *btnWidth;/**< btn宽度的数组 */
@property (nonatomic) UIButton *leftBtn;/**< 左边按钮 */
@property (nonatomic) UIButton *rightBtn;/**< 右边按钮 */

@end
@implementation CustomerNavigationBars

- (instancetype)initWithFrame:(CGRect)frame btnTitleArray:(NSArray *)btnTitleArray
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _cKwidth = frame.size.width;
        _cKheight = frame.size.height;
        self.titleArray = btnTitleArray;
        self.btnWidth = [NSMutableArray array];
        
        for (int i = 0; i < btnTitleArray.count; i ++) {
            //计算Btn宽度
            NSString *btnTitle = btnTitleArray[i];
            //当前btn宽度
            CGFloat btnWidth = [NSString sizeOfString:btnTitle fontSize:FONT_SIZE].width;
            
            [self.btnWidth addObject:@(btnWidth)];
            
            //创建btn
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(_cKwidth * 0.02 + CGRectGetMaxX(self.scrollView.subviews.lastObject.frame), _cKheight * 0.1, btnWidth + _cKwidth * 0.02, _cKheight * 0.8)];
            btn.titleLabel.font = [UIFont systemFontOfSize:FONT_SIZE];
            btn.tag = 1000 + i;
            
            [btn setTitle:btnTitle forState:UIControlStateNormal];
            [btn setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:@"二级菜单_未选中"] forState:UIControlStateNormal];
            [btn setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:@"二级菜单_选中"] forState:UIControlStateSelected];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(action_btnClick:) forControlEvents:UIControlEventTouchUpInside];
            
            [self.scrollView addSubview:btn];
        }
        self.scrollView.contentSize = CGSizeMake(CGRectGetMaxX(self.scrollView.subviews.lastObject.frame), _cKheight);
        [self addSubview:self.scrollView];
        
    }
    
    
    //按钮
    [self addSubview:self.leftBtn];
    [self addSubview:self.rightBtn];
    
    
    //按钮状态判断
    if (self.scrollView.contentSize.width > KWIDTH) {
        self.rightBtn.hidden = NO;
    }
    return self;
}

#pragma mark - action_BtnClick
- (void)action_btnClick:(UIButton *)sender{
    //点击按钮时触发协议方法
    if (self.delegate) {
        [_delegate customeNavigationBarView:self didSelectButton:sender];
    }
    
    //处理选中状态
    NSArray *ar = self.scrollView.subviews;
    for (int i = 0; i < self.titleArray.count; i ++) {
        ((UIButton *)ar[i]).selected = NO;
    }
    
    sender.selected = YES;
    
    NSInteger index = sender.tag - 1000;
    
    //获取屏幕数
    NSInteger number = (self.scrollView.contentSize.width / _cKwidth) + 1;
    
    //计算大约一个屏幕上的按钮个数
    NSInteger btnNumber = self.titleArray.count / number;
    
    
    if (number == 1) {
        return;
    }

    if (index < (btnNumber / 2) + 1) {
        [self btn_animation:0];
    }
    if (index >= (btnNumber / 2) + 1 && index < self.titleArray.count - btnNumber / 2) {
        [self btn_animation:CGRectGetMaxX(sender.frame) / 2];
    }
    if (index >= (self.titleArray.count - btnNumber * (number - 1)) / 2) {
        [self btn_animation:self.scrollView.contentSize.width - _cKwidth * 0.9];
    }
}


#pragma mark - action
//左右按钮执行方法
- (void)action_BtnOffset:(UIButton *)sender{
    switch (sender.tag - 1000) {
        case 10:
        {//左
            [self btn_animation:self.scrollView.contentSize.width - _cKwidth * 0.9];
        }
            break;
        case 20:
        {//右
            [self btn_animation:0];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - 动画滑动方法
- (void)btn_animation:(CGFloat)offset{

    [UIView animateWithDuration:0.5 animations:^{
        self.scrollView.contentOffset = CGPointMake(offset, 0);
    }];
}


#pragma mark - UIScrollViewDelegate
//当滑条滑动执行方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x > 0) {
        self.leftBtn.hidden = NO;
    }else{
        self.leftBtn.hidden = YES;
    }
    
    if (scrollView.contentOffset.x < scrollView.contentSize.width) {
        self.rightBtn.hidden = NO;
    }else{
        self.rightBtn.hidden = YES;
    }
    
}

#pragma mark - getter
- (UIScrollView *)scrollView{
    if (!_scrollView ) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(_cKwidth * 0.05, 0, _cKwidth * 0.9, _cKheight)];
        _scrollView.delegate = self;
        _scrollView.bounces = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

- (UIButton *)rightBtn{
    if (!_rightBtn) {
        _rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(_cKwidth * 0.95, 0, 30, 50)];
        [_rightBtn setImage:[LocalDataManager getImageWithSourceOfPath:@"菜单右拉"] forState:UIControlStateNormal];
        _rightBtn.tag = 1000 + 10;
        [_rightBtn addTarget:self action:@selector(action_BtnOffset:) forControlEvents:UIControlEventTouchUpInside];
        _rightBtn.hidden = YES;
    }
    return _rightBtn;
}

- (UIButton *)leftBtn{
    if (!_leftBtn) {
        _leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(_cKwidth * 0.03, 0, 30, 50)];
        [_leftBtn setImage:[LocalDataManager getImageWithSourceOfPath:@"菜单左拉"] forState:UIControlStateNormal];
        _leftBtn.tag = 1000 + 20;
        [_leftBtn addTarget:self action:@selector(action_BtnOffset:) forControlEvents:UIControlEventTouchUpInside];
        _leftBtn.hidden = YES;
    }
    return _leftBtn;
}
    
@end
